/*
 * @Author: szx
 * @Date: 2022-03-29 13:21:54
 * @LastEditTime: 2022-03-29 13:41:14
 * @Description:
 * @FilePath: \leetcode\剑指offer\62\62.js
 */
/**
 * @param {number} n
 * @param {number} m
 * @return {number}
 */
var lastRemaining = function (n, m) {
    if (n == 1) return 0;
    return (lastRemaining(n - 1, m) + m) % n;
};
var lastRemaining = function (n, m) {
    let f = 0; //最后剩下一个人的情况时胜利者的下标是0
    //开始逆推
    for (let i = 2; i <= n; ++i) {
        f = (f + m) % i; //循环右移m位
    }
    return f;
};
